const Router = require('koa-router')
const ArticleModel = require('../models/article')
const ArticleCommon = require('../common/article')
const route = new Router({ prefix: '/article' })

// 文章编辑页面
route.get('/articleEdit', async (ctx) => {
    let data = {
        isLogin: ctx.session.user === true,
        title: '文章编辑页面',
        article: {}
    }
    const { id } = ctx.query
    if (id) {
        data.article = await ArticleModel.findById(id)
    }
    await ctx.render('article-edit', data)
})

/*
    创建文章
        url: /article
        method: PUT
        params: {
            title
            category
            html_body
            text_body
            createDate
        }
*/
route.put('/', async (ctx) => {
    let res = {
        success: false,
        msg: '',
        error: null,
        data: {}
    }
    const { title,
        category,
        html_body,
        text_body, } = ctx.request.body
    
    let params = [
        title,
        category,
        html_body,
        text_body,
    ]
    if (params.indexOf('') === -1 &&
        params.indexOf(null) === -1 &&
        params.indexOf(undefined) === -1
    ) {
        try {
            res.data = await ArticleModel.create({
                title,
                category,
                html_body,
                text_body,
            })
            res.success = true
        } catch (error) {
            ctx.status = 500
        }
    } else {
        ctx.status = 401
        res.error = '字段不能为空'
    }
    ctx.body = res
})


/*
    修改文章
        url: /article
        method: POST
        params: {
            title
            category
            html_body
            text_body
            createDate
        }
*/
route.post('/', async (ctx) => {

    let res = {
        success: false,
        msg: '',
        error: null,
        data: {}
    }
    const { title,
        category,
        html_body,
        text_body, 
        id 
    } = ctx.request.body
    
    let params = [
        title,
        category,
        html_body,
        text_body,
        id
    ]
    if (params.indexOf('') === -1 &&
        params.indexOf(null) === -1 &&
        params.indexOf(undefined) === -1
    ) {
        try {
            let article = await ArticleModel.findById(id)
            article.set({
                title,
                category,
                html_body,
                text_body, 
            })
            await article.save()    
            res.success = true
        } catch (error) {
            res.error = error
            ctx.status = 500
        }
    } else {
        res.error = '字段不能为空'
        ctx.status = 401
    }
    ctx.body = res
})

/*
    删除文章
    url: /article
    method: DELETE
    params: {
        id
    }
*/
route.delete('/', async (ctx) => {
    let res = {
        success: false,
        msg: '',
        error: null,
        data: {}
    }
    const { id } = ctx.query
    if (id) {
        try {
            let article = await ArticleModel.findById(id)
            article.set({
                isDelete: true
            })
            await article.save()    
            res.success = true
        } catch (error) {
            res.error = error
            ctx.status = 500
        }
    } else {
        res.error = 'id 不能为空'
        ctx.status = 401
    }
    ctx.body = res
})

// 查询文章
// url: /article
// method: GET
// params: {
//     category
// }
// 文章分类
// 排序
route.get('/', async (ctx) => {
    const { category } = ctx.query
    let data = {
        isLogin: ctx.session.user === true,
        title: '文章分类',
        articleList: [],
        categoryList: [],
        category
    }
    let condition = {category}
    if (category === 'all' || !category) {
        condition = {}
    }
    data.articleList = await ArticleCommon.find(condition)

    data.categoryList = await ArticleModel.aggregate()
        .match({ isDelete: { $eq: false } }) // 过滤已删除的文章
        .group({ _id: '$category'})
    
    await ctx.render('article-category', data)
})

module.exports = route
